<?php
class Encounter extends ORDataObject {
	var $encounter_id				=	'';
	var $medical_record_id			=	'';
	var $symptom					=	'';
	var $left_intraocular_tension	=	'';
	var $right_intraocular_tension	=	'';
	var $left_ban_phan_truoc		=	'';
	var $left_ban_phan_sau			=	'';
	var $right_ban_phan_truoc		=	'';
	var $right_ban_phan_sau			=	'';
	var $chan_doan					=	'';
	var $primary_icd10				=	'';
	var	$secondary_icd10_1			=	'';
	var	$secondary_icd10_2			=	'';
	var	$treatment					=	'';
	var $note						=	'';
	var $doctor_id					=	'';
	var $doctor_updated_id          =   '';
	var $encounter_date             =   '';
	var $last_updated               =   ''; 
	
	var $_table	=	'encounter';
	var $_key	=	'encounter_id';
	var	$_foreignKeyList	=	array(
		'primary_icd10'		=>	'icd10',
		'secondary_icd10_1'	=>	'icd10',
		'secondary_icd10_2'	=>	'icd10',
		'medical_record_id'	=>	'medical_record'
	);
	
	const ENC_TREA_SURGERY			=	'surgery';
	const ENC_TREA_MOVE				=	'move_to_other_hospital';
	const ENC_TREA_PRESCRIPTION		=	'prescription';
	
	public function Encounter(){
		parent::ORDataObject();
	}
	
	public function setup($id = 0){
		$this->set('id',$id);
		if($id>0)
			$this->populate();
	}
	
	public function populate(){
		parent::populate('encounter_id');
	}
	
	public function setData($record_id, $symptom, $leftTension, $rightTension, $left_BPT, $left_BPS, 
		$right_BPT, $right_BPS, $chan_doan, $primary_icd10, $secondary_icd10_1, $secondary_icd10_2,
		$treatment, $note, $doctor_id){
			$data	=	array(
				'medical_record_id'			=>	$record_id,
				'symptom'					=>	$symptom,
				'left_intraocular_tension'	=>	$leftTension,
				'right_intraocular_tension'	=>	$rightTension,
				'left_ban_phan_truoc'		=>	$left_BPT,
				'left_ban_phan_sau'			=>	$left_BPS,
				'right_ban_phan_truoc'		=>	$right_BPT,
				'right_ban_phan_sau'		=>	$right_BPS,
				'chan_doan'					=>	$chan_doan,
				'primary_icd10'				=>	$primary_icd10,
				'secondary_icd10_1'			=>	$secondary_icd10_1,
				'secondary_icd10_2'			=>	$secondary_icd10_2,
				'treatment'					=>	$treatment,
				'note'						=>	$note,
				'doctor_id'					=>	$doctor_id,
				
				'encounter_date'			=>  $this->getDbCreateDate()
			);
			$this->populateArray($data);
	}
	
	public function  setId($id){
		$this->encounter_id=$id;
	}
	
	public function getId(){
		return $this->encounter_id;
	}
	
	public function getEncounterTreatmentList(){
		$list	=	array(
			self::ENC_TREA_PRESCRIPTION	=>	L10nHelper::_(self::ENC_TREA_PRESCRIPTION),
			self::ENC_TREA_SURGERY		=>	L10nHelper::_(self::ENC_TREA_SURGERY),
			self::ENC_TREA_MOVE			=>	L10nHelper::_(self::ENC_TREA_MOVE)
		);
		return $list;
	}
	
	public function getSymptom(){
		return $this->symptom;
	}
	
	public function getLeftTension(){
		return $this->left_intraocular_tension;
	}
	
	public function getRightTension(){
		return $this->right_intraocular_tension;
	}
	
	public function getLeftBPT(){
		return $this->left_ban_phan_truoc;
	}
	
	public function getLeftBPS(){
		return $this->left_ban_phan_sau;
	}
	
	public function getRightBPT(){
		return $this->right_ban_phan_truoc;
	}
	
	public function getRightBPS(){
		return $this->right_ban_phan_sau;
	}
	
	public function getPrimaryIcd10(){
		return $this->primary_icd10;
	}
	
	public function getChanDoan(){
		return $this->chan_doan;
	}
	
	public function getPrimaryDiagnosis(){
		return '';
	}
	
	public function getSecondaryIcd10_1(){
		return $this->secondary_icd10_1;
	}
	
	public function getSecondaryIcd10_2(){
		return $this->secondary_icd10_2;
	}
	
	public function getSecondaryDiagnosis_1(){
		return '';
	}
	
	public function getSecondaryDiagnosis_2(){
		return '';
	}

	public function getNote(){
		return $this->note;
	}
	
	public function getTreatment(){
		return $this->treatment;
	}
	
	public function getTreatmentText($culture = 'vi-VN'){
		$treatments	=	$this->getEncounterTreatmentList($culture);
		if(!empty($this->treatment) && array_key_exists($this->treatment,$treatments)){
			return $treatments[$this->treatment];
		}
		return '';
	}
	
	public function getObjectFromRecordId($recordId) {		
		$sql = "SELECT $this->_key FROM $this->_table WHERE medical_record_id = $recordId";
		$res = $this->_execute($sql);
		
		$obj = ORDataObject::factory('Encounter');
		if (!$res->EOF) {
			$obj->setup($res->fields[$this->_key]);
		}
		return $obj;					
	}
	
	public function getDoctorId(){
		return $this->doctor_id;
	}
	
	public function getDoctorName(){
		$person	=	ORDataObject::factory('Employee');
		$person->setup($this->doctor_id);
		return $person->getName();
	}
	
	public function getUpdatedDoctorName(){
		$person	=	ORDataObject::factory('Person');
		$person->setup($this->doctor_updated_id);
		return $person->getName();
	}
	
	public function getDate($whichDate) {
		if ($whichDate == 'created') {
			$sql = "SELECT encounter_date from $this->_table WHERE encounter_id = $this->encounter_id";
			$res = $this->_execute($sql);
			$date = $res->fields['encounter_date'];
		} else {
			$sql = "SELECT last_updated from $this->_table WHERE encounter_id = $this->encounter_id";
			$res = $this->_execute($sql);
			$date = $res->fields['last_updated'];
		}
		
		return L10nHelper::renderDateTime($date);		 
	}
	
	public function getDbCreateDate() {
		$sql = "SELECT encounter_date from $this->_table WHERE encounter_id = $this->encounter_id";
		$res = $this->_execute($sql);
		if (!empty($res)) {
			return $res->fields['encounter_date'];
		} else {
			return "1970-01-01 01:01:01";
		}
	}
	
	function persist($encounter_date='', $last_updated='') {		
		$res = parent::persist();
				
		$sql = "UPDATE $this->_table SET last_updated = NOW() WHERE $this->_key = " . $this->get_id();
		$this->_execute($sql);
		if ($encounter_date == '') {
			$sql = "UPDATE $this->_table SET encounter_date = NOW() WHERE $this->_key = " . $this->get_id();
			$this->_execute($sql);
		}
		return $res;
	}
	
	
	public function updateDoctorId($doctorId, $encId) {
		$this->_execute("UPDATE $this->_table SET doctor_id = $doctorId WHERE encounter_id = $encId");
	}
	
	public function delete($encounterId) {
		$this->setup($encounterId);
		$medicalRecordId = $this->get('medical_record_id');
			
		// delete encounter_common_info
		$sql = "DELETE FROM encounter_common_info WHERE medical_record_id = $medicalRecordId";
		$this->_execute($sql);
		
		// delete encounter_refraction
		$sql = "DELETE FROM encounter_refraction WHERE medical_record_id = $medicalRecordId";
		$this->_execute($sql);				
		
		// delete encounter
		$sql = "DELETE FROM encounter WHERE medical_record_id = $medicalRecordId";
		$this->_execute($sql);								
	}
}
?>